package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductSkuProValue;
import com.erp.gather.domain.ErpCollectProductSkuProperty;
import com.erp.gather.domain.bo.ErpCollectProductSkuPropertyBo;
import com.erp.gather.domain.vo.ErpCollectProductSkuPropertyVo;
import com.erp.gather.mapper.ErpCollectProductSkuProValueMapper;
import com.erp.gather.mapper.ErpCollectProductSkuPropertyMapper;
import com.erp.gather.service.IErpCollectProductSkuPropertyService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 采集商品SKU属性Service业务层处理
 *
 * @author erp
 * @date 2023-03-07
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductSkuPropertyServiceImpl implements IErpCollectProductSkuPropertyService {

	private final ErpCollectProductSkuPropertyMapper baseMapper;
	private final ErpCollectProductSkuProValueMapper skuProValueMapper;

	/**
	 * 查询采集商品SKU属性
	 */
	@Override
	public ErpCollectProductSkuPropertyVo queryById(Long id) {
		return baseMapper.selectVoById(id);
	}

	/**
	 * 查询采集商品SKU属性列表
	 */
	@Override
	public TableDataInfo<ErpCollectProductSkuPropertyVo> queryPageList(ErpCollectProductSkuPropertyBo bo,
																	   PageQuery pageQuery) {
		LambdaQueryWrapper<ErpCollectProductSkuProperty> lqw = buildQueryWrapper(bo);
		Page<ErpCollectProductSkuPropertyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
		return TableDataInfo.build(result);
	}

	/**
	 * 查询采集商品SKU属性列表
	 */
	@Override
	public List<ErpCollectProductSkuPropertyVo> queryList(ErpCollectProductSkuPropertyBo bo) {
		LambdaQueryWrapper<ErpCollectProductSkuProperty> lqw = buildQueryWrapper(bo);
		return baseMapper.selectVoList(lqw);
	}

	private LambdaQueryWrapper<ErpCollectProductSkuProperty> buildQueryWrapper(ErpCollectProductSkuPropertyBo bo) {
		LambdaQueryWrapper<ErpCollectProductSkuProperty> lqw = Wrappers.lambdaQuery();
		lqw.like(StringUtils.isNotBlank(bo.getPropertyName()), ErpCollectProductSkuProperty::getPropertyName,
				bo.getPropertyName());
		return lqw;
	}

	/**
	 * 新增采集商品SKU属性
	 */
	@Override
	public Boolean insertByBo(ErpCollectProductSkuPropertyBo bo) {
		ErpCollectProductSkuProperty add = BeanUtil.toBean(bo, ErpCollectProductSkuProperty.class);
		validEntityBeforeSave(add);

		// 获取是否有该属性
		ErpCollectProductSkuProperty property = baseMapper
				.selectOne(new LambdaQueryWrapper<ErpCollectProductSkuProperty>()
						.eq(ErpCollectProductSkuProperty::getPropertyName, add.getPropertyName()).last("limit 1"));
		if (property == null) {
			boolean flag = baseMapper.insert(add) > 0;
			if (flag) {
				bo.setId(add.getId());
			}
			return flag;
		} else {
			bo.setId(property.getId());
			return true;
		}
	}

	/**
	 * 修改采集商品SKU属性
	 */
	@Override
	public Boolean updateByBo(ErpCollectProductSkuPropertyBo bo) {
		ErpCollectProductSkuProperty update = BeanUtil.toBean(bo, ErpCollectProductSkuProperty.class);
		validEntityBeforeSave(update);
		return baseMapper.updateById(update) > 0;
	}

	/**
	 * 保存前的数据校验
	 */
	private void validEntityBeforeSave(ErpCollectProductSkuProperty entity) {
		// TODO 做一些数据校验,如唯一约束
	}

	/**
	 * 批量删除采集商品SKU属性
	 */
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			// TODO 做一些业务上的校验,判断是否需要校验
		}
		return baseMapper.deleteBatchIds(ids) > 0;
	}

	/**
	 * @Description: 根据属性值，获取属性ID
	 * @Author: j.zh
	 * @Date: 2024/6/12
	 * @param type
	 * @param value
	 * @return
	 * @Version: 4.0.0
	 **/
	@Override
	public String getValueById(String type, String value){
		// 根据类型，查询ID
		if(type.equals("P")){
			ErpCollectProductSkuProperty skuProperty = baseMapper.selectOne(new LambdaQueryWrapper<ErpCollectProductSkuProperty>()
					.eq(ErpCollectProductSkuProperty::getPropertyName, value));
			if (skuProperty != null){
				return skuProperty.getId().toString();
			}else {
				// 新增
				skuProperty = new ErpCollectProductSkuProperty();
				skuProperty.setPropertyName(value);
				baseMapper.insert(skuProperty);
				return skuProperty.getId().toString();
			}
		}else {
			ErpCollectProductSkuProValue skuProValue = skuProValueMapper.selectOne(new LambdaQueryWrapper<ErpCollectProductSkuProValue>()
					.eq(ErpCollectProductSkuProValue::getPropertyValue, value));
			if (skuProValue != null){
				return skuProValue.getId().toString();
			}else {
				// 新增
				skuProValue = new ErpCollectProductSkuProValue();
				skuProValue.setPropertyValue(value);
				skuProValueMapper.insert(skuProValue);
				return skuProValue.getId().toString();
			}
		}
	}
}
